package com.example.administrator.shzs.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.administrator.shzs.R;
import com.example.administrator.shzs.parse.ParseUserData;
import com.example.administrator.shzs.app.MyApplication;
import com.example.administrator.shzs.url.Contents;
import com.example.administrator.shzs.entity.User;
import com.example.administrator.shzs.fragment.order.AllOrderFragment;
import com.example.administrator.shzs.fragment.order.StayEvaluateFragment;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;


/**
 * 订单 Fragment
 *
 */
public class OrderFragment extends Fragment {

    private List<Fragment> fragments;

    private List<String> mTitles;

    private ViewPager vpOrder;
    private TabLayout mTabLayout;

    //实体类
    private User userdata;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order, null);

        setViews(view);

        return view;
    }

    private void inidataFromWeb() {
        String url = Contents.USER + MyApplication.app.getUserId();
        Request request = new Request.Builder().url(url).build();
        Contents.okhttp.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String request = response.body().string();
                userdata = ParseUserData.getUser(request);
                //将数据绑定到变量上面
                String point = userdata.getPoint(); //积分
                String name = userdata.getUsername();//姓名
                String phone = userdata.getPhone();//手机号
                //将数据保存到全局
                MyApplication.app.setPoint(point);
                MyApplication.app.setUsername(name);
                MyApplication.app.setPhone(phone);
                //持久化保存数据
                MyApplication.app.saveUserData();
//                //全局获取用户名与积分
//                MyApplication.app.setUsername(userdata.getUsername());
//                MyApplication.app.setPoint(userdata.getPoint());
//                //持久化保存
//                MyApplication.app.saveUserData();
            }
        });
    }



    //页面切换发送一条广播
    @Override
    public void onHiddenChanged(boolean hidden) {

//        如果切换当前fragment就去发送一条广播
        if (!hidden){
            Intent intent = new Intent();
            intent.setAction("1");
            getActivity().sendBroadcast(intent);
            if (MyApplication.app.getPoint() == null){
                inidataFromWeb();
            }
        }
        super.onHiddenChanged(hidden);
    }

    //登录页面退出之后发送广播
    @Override
    public void onResume() {
        super.onResume();
        Intent intent = new Intent();
        intent.setAction("1");
        getActivity().sendBroadcast(intent);
    }

    private void setViews(View view) {

        vpOrder= (ViewPager) view.findViewById(R.id.vp_order);
        mTabLayout= (TabLayout) view.findViewById(R.id.tab_layout_order);

        mTitles=new ArrayList<>();
        mTitles.add("全部订单");
        mTitles.add("待评价");

        fragments=new ArrayList<>();
        fragments.add(new AllOrderFragment());
        fragments.add(new StayEvaluateFragment());

        MyPagerAdapter adapter=new MyPagerAdapter(getChildFragmentManager());
        vpOrder.setAdapter(adapter);

        mTabLayout.setupWithViewPager(vpOrder);

    }



    class MyPagerAdapter extends FragmentPagerAdapter{

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {

            return fragments.size();

        }

        @Override
        public CharSequence getPageTitle(int position) {

            return mTitles.get(position);
        }
    }

}
